﻿using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Web;
using System.Web.Security;
using Biljart.Model.Dao;
using Biljart.Model.Util;
using Biljart.Model.Validator;

namespace Biljart.Model.Service
{
    public class UserService : IUserService
    {
        private readonly IUserDao _userDao;
        private readonly UserValidator _validator;

        public UserService()
        {
            _userDao = new UserDao();
            _validator = new UserValidator();
        }

        public User GetUser(long id)
        {
            return _userDao.GetUser(id);
        }

        public IEnumerable<User> GetUsers()
        {
            return _userDao.GetUsers();
        }

        public IEnumerable<User> GetUsers(HashSet<long> ids)
        {
            return _userDao.GetUsers(ids);
        }

        public bool AddUser(User user, string password, string email)
        {
            if (HttpContext.Current.User.IsInRole("admin"))
            {
                if (_validator.ValidateUser(user))
                {
                    Membership.CreateUser(user.Nickname, password, email);
                    return _userDao.AddUser(user);
                }
            }
            return false;
        }

        public bool LogIn(string nickname, string password)
        {
            var user = _userDao.GetUser(nickname);
            if (user != null)
            {
                //todo handle the login
                return true;
            }

            return false;
        }
    }
}
